package com.inspector.app.ui.flight.mapsearch;


import android.graphics.Color;
import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * [地图搜索数据整理]
 *
 * @author pank 2023/3/26
 */
@SuppressWarnings("ALL")
public class MapSearchInstance {

    private String TAG = "MapSearchInstance";

    //[获取重复的字符串集合权重] 2023/12/5 排序优化
    public static Integer containsSearchWeight(String name, String searchTxt) {
        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(searchTxt)) return 0;
        try {
            if (name.equals(searchTxt)) {
                return Integer.MAX_VALUE;
            }
            String allChars = name.toUpperCase();
            if (allChars.equals(searchTxt.toUpperCase())) {
                return Integer.MAX_VALUE - 1;
            }

            //[编辑距离算法计算相似度,解决不了排序问题]
            /*double similarity = LevenshteinKt.getSimilarity(searchTxt, name);*/

            int weight = 0;
            String search = searchTxt.toUpperCase();
            char[] searchChars = search.toCharArray();
            for (int length = searchChars.length; length > 0; length--) {
                String substring = search.substring(0, length);
                if (allChars.startsWith(substring)) {
                    weight = 100 * length;
                    break;
                }
            }

            int charCount = 0;
            ArrayList<Character> characters = containsList(name, searchTxt);
            int count = characters.size();
            while (count != 0) {
                Character character = characters.get(characters.size() - count);
                if (allChars.contains(character.toString())) {
                    Pattern pattern = Pattern.compile(character.toString());
                    Matcher matcher = pattern.matcher(new SpannableString(name.toUpperCase()));
                    while (matcher.find()) {
                        charCount += 1;
                    }
                }
                count--;
            }
            return weight + charCount;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }


    /**
     * @param matchTxt  匹配文本
     * @param searchTxt 输入框搜索文本
     * @return
     */
    public static SpannableStringBuilder getColorSpan(String matchTxt, String searchTxt) {
        SpannableStringBuilder spannableString = new SpannableStringBuilder(matchTxt);
        if (TextUtils.isEmpty(searchTxt)) return spannableString;

        Integer searchWeight = containsSearchWeight(matchTxt, searchTxt);
        if (searchWeight == Integer.MAX_VALUE) {//[完全匹配]
            Pattern pattern = Pattern.compile(searchTxt);
            Matcher matcher = pattern.matcher(new SpannableString(matchTxt.toUpperCase()));
            while (matcher.find()) {
                spannableString.setSpan(new ForegroundColorSpan(
                                Color.parseColor("#529fe5")),
                        matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
            }
        } else {//[局部匹配]
            ArrayList<Character> characters = containsList(matchTxt, searchTxt);
            int count = characters.size();
            while (count != 0) {
                Character character = characters.get(characters.size() - count);
                if (matchTxt.toUpperCase().contains(character.toString())) {
                    Pattern pattern = Pattern.compile(character.toString());
                    Matcher matcher = pattern.matcher(new SpannableString(matchTxt.toUpperCase()));
                    while (matcher.find()) {
                        spannableString.setSpan(new ForegroundColorSpan(
                                        Color.parseColor("#529fe5")),
                                matcher.start(), matcher.end(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }
                }
                count--;
            }
        }
        return spannableString;
    }

    //[判断匹配文本是否包含搜索文字各个字符]
    private static ArrayList<Character> containsList(String matchTxt, String searchTxt) {
        ArrayList<Character> objects = new ArrayList<>();
        try {
            String allChars = matchTxt.toUpperCase();
            char[] searchChars = searchTxt.toUpperCase().toCharArray();
            for (char searchChar : searchChars) {
                int index = allChars.indexOf(searchChar);
                if (index != -1) {
                    objects.add(searchChar);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return objects;
    }


    //[单例类]
    private volatile static MapSearchInstance mInstance;

    public static synchronized MapSearchInstance getInstance() {
        if (mInstance == null) {
            synchronized (MapSearchInstance.class) {
                if (mInstance == null) {
                    mInstance = new MapSearchInstance();
                }
            }
        }
        return mInstance;
    }

    public static void resetInstance() {
        mInstance = null;
    }

}
